package com.example.familytreebe.controller;

import com.example.familytreebe.entity.RelationshipType;
import com.example.familytreebe.service.RelationshipTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/relationship-types")
public class RelationshipTypeController {
    private final RelationshipTypeService relationshipTypeService;

    @Autowired
    public RelationshipTypeController(RelationshipTypeService relationshipTypeService) {
        this.relationshipTypeService = relationshipTypeService;
    }

    @PostMapping
    public RelationshipType saveRelationshipType(@RequestBody RelationshipType relationshipType) {
        return relationshipTypeService.saveRelationshipType(relationshipType);
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getRelationshipTypeById(@PathVariable Long id) {
        Optional<RelationshipType> relationshipType = relationshipTypeService.getRelationshipTypeById(id);
        return relationshipType.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }

    @GetMapping
    public List<RelationshipType> getAllRelationshipTypes() {
        return relationshipTypeService.getAllRelationshipTypes();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteRelationshipType(@PathVariable Long id) {
        relationshipTypeService.deleteRelationshipType(id);
        return ResponseEntity.ok("关系类型删除成功");
    }
}